import service from "@/api/service";
export default {
  namespaced: true,
  state: {
    queryObject: {
      pageIndex: 1,
      pageSize: 20,
      sortFields: {
        "CreateTime": true,
      },
      parameters: {
        keyword: null,
      },
    },
    dataTable: {
      result: [],
      pageIndex: 1,
      pageSize: 20,
      pageCount: 0,
      rowCount: 0,
    },
    currentItem: {
      appId: null,
      appName: null,
      domain: null,
      port: null,
      ssl: null,
      path: null,
      icon: null,
      fullHost: null,
      fullPath: null,
      createTime: null,
      updateTime: null
    },
  },
  mutations: {
    setData(state, dataTable) {
      state.dataTable = dataTable;
    },
    selectItem(state, item) {
      state.currentItem = item;
    },
    setParameters(state, parameters) {
      for (let name in parameters) {
        state.queryObject.parameters[name] = parameters[name];
      }
    },
  },
  actions: {
    query({ commit, state }) {
      return service.app
        .query({ data: state.queryObject })
        .then((response) => {
          commit("setData", response.success ? response.data : null);
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    create({ commit, state, dispatch }, dto) {
      return service.app
        .create({ data: dto })
        .then((response) => {
          if (response.success) dispatch("query");
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    update({ commit, state, dispatch }, dto) {
      return service.app
        .update({ data: dto })
        .then((response) => {
          if (response.success) dispatch("query");
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    delete({ commit, state, dispatch }, id) {
      return service.app
        .delete({ data: id })
        .then((response) => {
          if (response.success) dispatch("query");
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    item({ commit, state }, id) {
      return service.app
        .item({ params: { id } })
        .then((response) => {
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    toggle({ dispatch, state }, id) {
      return service.app
        .toggle({ params: { id } })
        .then((response) => {
          if (response.success) {
            return response;
          } else {
            throw response.message;
          }
        })
        .catch((error) => {
          return error;
        });
    }
  },
};
